package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Area;
import com.blb.service.IAreaService;
import com.blb.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *库区管理
 * @author wss
 * @since 2020-05-21
 */
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private IAreaService areaService;

    /**
     * 分页查询货区列表
     * @param cTime
     * @param wid
     * @param aid
     * @param draw
     * @param start
     * @param length
     * @return
     */
    @RequiresPermissions("story:area")
    @RequestMapping("cargoArea_manage")
    public DataTablesPage<Area> cargoArea_manage(String cTime, String wid, String aid, Integer draw, Integer start, Integer length) {
        String companyId = ShiroUtils.getCompany();
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
        if (StringUtils.isEmpty(cTime)){
            map.put("cTime",null);
        }else {
            map.put("cTime",'%'+cTime+"%");
        }
        map.put("wid", wid);
        map.put("aid", aid);
        int pageCount = areaService.getPageCount(map);
        map.put("start", start);
        map.put("length", length);
        List<Area> pageList = areaService.getPageList(map);
        return new DataTablesPage<>(draw, pageCount, pageCount, pageList);
    }

    /**
     * 添加货区
     * @param area
     * @return
     */
    @RequiresPermissions("storyarea:add")
    @PostMapping("add")
    public Response add(Area area) {
        area.setId(UUIDUtils.getId());
        area.setCompanyId(ShiroUtils.getCompany());
        area.setCreateTime(TimeUtils.getNowTime());
        areaService.save(area);
        return Response.success("添加成功");
    }

    /**
     * 修改货区
     * @param area
     * @return
     */
    @RequiresPermissions("storyarea:update")
    @PostMapping("update")
    public Response update(Area area) {
        areaService.updateById(area);
        return Response.success("修改成功");
    }

    /**
     * 修改货区状态
     * @param area
     * @return
     */
    @RequiresPermissions("storyarea:update")
    @PostMapping("updateStatus")
    public Response updateStatus(Area area) {
        Area pojo = areaService.getById(area.getId());
        pojo.setAreaStatus(area.getAreaStatus());
        areaService.updateById(pojo);
        return Response.success();
    }

    /**
     * 根据仓库id 公司id 过滤货区
     * @param warehouseId
     * @return
     */
    @RequiresPermissions("story:area")
    @RequestMapping("listByWid")
    public List<Area> listByWid(String warehouseId) {
        String companyId = ShiroUtils.getCompany();
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("warehouse_id", warehouseId);
        wrapper.eq("company_id", companyId);
        return areaService.list(wrapper);
    }


}

